package com.weave.set;

import com.weave.list.LinkedList;
import com.weave.list.List;

/**
 * set集合，ListSet使用LIst集合
 * @param <E>
 */
public class ListSet<E> implements Set<E> {
	private List<E> list = new LinkedList<>();

	@Override
	public int size() {
		return list.size();
	}

	@Override
	public boolean isEmpty() {
		return list.isEmpty();
	}

	@Override
	public void clear() {
		list.clear();
	}

	@Override
	public boolean contains(E element) {
		return list.contains(element);
	}

	@Override
	public void add(E element) {
		int index = list.indexOf(element);
		// 存在覆盖，不存在，添加
		if(index != List.ELEMENT_NOT_FOUND){
			list.set(index,element);
		}else{
			list.add(element);
		}
	}

	@Override
	public void remove(E element) {
		int index = list.indexOf(element);
		if(index != List.ELEMENT_NOT_FOUND){
			list.remove(index);
		}
	}

	@Override
	public void traversal(Visitor<E> visitor) {
		if(visitor == null) {
			return;
		}

		for (int i = 0; i < list.size(); i++) {
			if(visitor.visit(list.get(i))){
				return;
			}
		}
	}
}